package com.xe.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

public class XUtil {

	
	XMime mimeTypes = new XMime();
	XECodemirrorConfig editors = new XECodemirrorConfig();
	
	public boolean  isSource(String fileName){
    	  String mime=this.getMimeTypeByFileName(fileName);
    	  
    	  if(mime!=null && (
    			  mime.equalsIgnoreCase(XConstants.MIMETYPE_4_GROOVY) 
    			  || 
    			  mime.equalsIgnoreCase(XConstants.MIMETYPE_4_GROOVY_TEMPLATE)
    			  )) {
    		  return true;
    	   };
    		  
    	  return false;
    }
    
    public String getMimeTypeByFileName(String fileName){
    	
  	  String fname="";
  	  String ext="";
  	  int mid= fileName.lastIndexOf(".");
  	  fname=fileName.substring(0,mid);
  	  ext=fileName.substring(mid+1,fileName.length());  
  	  
  	  String mime=mimeTypes.getProperty(ext);
  	  
  	  return mime;
  	
      }

public String getEditor(String fileName){
    	
     	
  	  String fname="";
  	  String ext="";
  	  int mid= fileName.lastIndexOf(".");
  	  fname=fileName.substring(0,mid);
  	  ext=fileName.substring(mid+1,fileName.length());  
  	  
  	  String e=editors.getProperty(ext);
  	  
  	  return e;
  	
      }
    
public String getEditorMime(String fileName){
	
 	
	  String fname="";
	  String ext="";
	  int mid= fileName.lastIndexOf(".");
	  fname=fileName.substring(0,mid);
	  ext=fileName.substring(mid+1,fileName.length());  
	  
	  String e=editors.getMimeProperty(ext);
	  
	  return e;
	
    }
  
   
    public String getParentDirName(String fullPath,boolean isDirectory ){
    	if (fullPath.trim().length()==0) return "";
    	String dirName="";
    	if (isDirectory) {
    		int lastIndex = fullPath.lastIndexOf("/");
    	   	if (lastIndex==-1) return "";
    	
    	   	dirName=fullPath.substring(0,lastIndex);
    	
    	   	lastIndex = dirName.lastIndexOf("/");

    	   	if (lastIndex==-1) return "";

    	   	dirName=fullPath.substring(0,lastIndex);
    	}
    	else{
    		int lastIndex = fullPath.lastIndexOf("/");
    	   	if (lastIndex==-1) return "";
    	
    	   	dirName=fullPath.substring(0,lastIndex);
    	
    	}
    	   	
    	return dirName;
    }

    
    public String getDirName(String fullPath){
    	int lastIndex = fullPath.lastIndexOf("/");
    	if (lastIndex==-1) return "";
    	
    	String dirName=fullPath.substring(0,lastIndex);
    	return dirName;
    }
    
	
    public String getFileName(String fullPath){
    	  String dirName = this.getDirName(fullPath);
    	  String fname = fullPath.replaceFirst(dirName, ""); 
    	  fname = fname.replace("/","");
    	  int mid = fname.lastIndexOf(".");
    	  if (mid==-1) return "";
    	  fname  = fname.substring(0,mid);
    	  return fname;
    }

    public String getCompName(String projectName,String fullPath){
    	
       if(fullPath.length()==0) return projectName;	
  	  String dirName = this.getDirName(fullPath);
  	  
          
      if (dirName.equals(fullPath.replace("/", ""))){
    	  return projectName + "." + dirName;
      } 
  	  
  	  String fname = fullPath.replace(dirName, "");
  	  
  	         
  	  int mid = fname.lastIndexOf(".");
  	  if (mid>0) fname  = fname.substring(0,mid);
  	  
  	  if(fname.length()==0) return projectName;
  	  fname=fname.replace("/", "");
  	  if (fname.length()==0) fname="_";
  	  String cname= projectName + "." + dirName.replace("/", ".") +"."+ fname.replace("/", "");
  	  return cname;
  }
    
    public String getScriptName(String projectName,String fullPath){
    	  String cname= projectName + "." + fullPath.replace("/", ".") ;
    	  return cname;
    }
    
    
    
      public String getFileExt(String fullPath){
    	  String dirName = this.getDirName(fullPath);
    	  String fname = fullPath.replace(dirName, "");
    	  fname= fname.replace("/", "");
      	  String ext="";

      	  int mid = fname.lastIndexOf(".");
      	  if (mid==-1) return "";
      	  
      	  
      	  ext  = fname.substring(mid,fname.length());  
      	  ext  = ext.replace(".", "");
      	  return ext;
      }

	
	public static String capitalize(String s) {
	    if (s.length() == 0) return s;
	    return s.substring(0, 1).toUpperCase() + s.substring(1);
	}

	public Map<String, String> getMimeTypes() {
		return mimeTypes.mime;
	}

	
}
